#include "select-level-scene.h"
#include "ui_select-level-scene.h"

SelectLevelScene::SelectLevelScene(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SelectLevelScene)
{
    initUi();

    initData();

    initUiData();

    initConnect();
}

SelectLevelScene::~SelectLevelScene()
{
    delete ui;

    delete back_button;
}


void SelectLevelScene::paintEvent(QPaintEvent *event)
{
    // 创建画家对象
    QPainter painter(this);

    // 加载主界面背景图片
    QPixmap pixmap_background(SELECT_LEVEL_SCENE);
    painter.drawPixmap(0, 0, pixmap_background);

    // 加载主界面游戏标题
    QPixmap pixmap_title(TITLE);
    pixmap_title = pixmap_title.scaled( pixmap_title.width() * 1, pixmap_title.height() * 1);
    painter.drawPixmap(20, 30, pixmap_title);
}


void SelectLevelScene::initUi()
{
    ui->setupUi(this);

    // 创建返回按钮
    this->back_button = new ClickButton(BACK_BUTTON_NORMAL, BACK_BUTTON_SELECTED, BACK_BUTTON_SOUND);
    this->back_button->setParent(this);
    this->back_button->move(this->width() - back_button->width() - 20, this->height() - back_button->height() - 20);

    // 创建关卡选择按钮
    for (int i = 0; i < 20; i++) {
        QLabel *level_label = createLevelLabel(i + 1);
        QPushButton *level_button = createLevelButton(LEVEL_ICO);

        level_label->move((i % 4) * 50 + 53, (i / 4) * 60 + 150);
        level_button->move((i % 4) * 50 + 53, (i / 4) * 60 + 150);

        connect(level_button, &QPushButton::clicked, this, [=]() {
            PlayGameScene *play_game_scene = new PlayGameScene(i + 1, config.data[i + 1]);
            play_game_scene->setParent(this->parentWidget());

            // 隐藏当前场景, 显示游戏关卡
            this->hide();
            play_game_scene->show();

            connect(play_game_scene->back_button, &QPushButton::clicked, this, [=]() {
                this->show();
                play_game_scene->hide();

                delete play_game_scene;
            });
        });
    }
}


void SelectLevelScene::initData()
{

}

void SelectLevelScene::initUiData()
{
    // 设置窗口固定大小
    this->setFixedSize(315, 560);

    // 设置窗口标题
    this->setWindowTitle("Select Level");

    // 设置窗口图标
    this->setWindowIcon(QPixmap(GOLD_COIN));
}

void SelectLevelScene::initConnect()
{

}

QPushButton *SelectLevelScene::createLevelButton(QString image_pathname)
{
    QPushButton *level_button = new QPushButton(this);

    QPixmap pixmap;
    bool result = pixmap.load(image_pathname);

    if(!result) {
        qDebug() << "加载图片 " << image_pathname << " 失败!";
        return nullptr;
    }

    // 图片设置缩放比
    pixmap = pixmap.scaled(pixmap.width() * 1, pixmap.height() * 1);

    // 设置按钮的大小
    level_button->setFixedSize(pixmap.size());

    // 设置不规则的图片样式
    level_button->setStyleSheet("QPushButton{border:0px;}");

    // 设置图标
    level_button->setIcon(pixmap);

    // 设置图标大小
    level_button->setIconSize(pixmap.size());

    return level_button;
}

QLabel *SelectLevelScene::createLevelLabel(int level)
{
    QLabel *level_label = new QLabel(this);

    // 设置标签大小
    level_label->setFixedSize(57, 57);

    // 设置标签内容
    level_label->setText(QString::number(level));

    // 设置标签内容为水平居中和垂直居中
    level_label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

    // 标签设置为鼠标穿透
    level_label->setAttribute(Qt::WA_TransparentForMouseEvents);

    return level_label;
}
